package h0719;

import java.util.ArrayDeque;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
  * @description 括号匹配
  * @author 不知名帅哥
  * @date 2024/7/19 21:41
  * @version 1.0
*/
public class BracketMatch {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s = sc.nextLine();
        sc.close();
        Pattern p = Pattern.compile("[()\\[\\]{}]");  //这里可以用replaceAll代替非括号的字符
        Matcher matcher = p.matcher(s);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()){
            sb.append(matcher.group());
        }
        String string = sb.toString();
//        System.out.println(string);
        System.out.println(getResult(string));
    }

    private static boolean getResult(String string) {
        if (string.isEmpty()){
            return true;
        }
        ArrayDeque<Character> stack=new ArrayDeque<>();  //也可以使用LinkedList.所有的括号组合使用一个map来存储。
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (c =='('||c=='['||c=='{'){
                stack.push(c);
            }else if (c==')'&&(stack.isEmpty()||stack.pop()!='(')){  //得考虑到第一个是后括号的情况。
                return false;
            }else if (c==']'&&(stack.isEmpty()||stack.pop()!='[')){
                return false;
            }else if (c=='}'&&(stack.isEmpty()||stack.pop()!='{')){
                return false;
            }
        }
        return stack.isEmpty();
    }
}
